package com.cuiweiyou.vigilesmicrecord.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuiweiyou.vigilesmicrecord.R;

import java.io.File;
import java.util.List;

/**
 * Created by macpro on 2018/4/27.
 */

public class SoundListAdapter extends RecyclerView.Adapter<SoundListAdapter.SoundHolder> {
    private List<String> list;
    private OnPlayClickListener playClickListener;
    private OnNameClickListener nameClickListener;
    private OnNameLongClickListener nameLongClickListener;

    public SoundListAdapter(List<String> list, OnPlayClickListener listener1, OnNameClickListener listener2, OnNameLongClickListener listener3) {
        this.list = list;
        this.playClickListener = listener1;
        this.nameClickListener = listener2;
        this.nameLongClickListener = listener3;
    }

    @Override
    public SoundHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_sound_list, null);
        return new SoundHolder(view);
    }

    @Override
    public void onBindViewHolder(SoundHolder holder, int position) {
        holder.indexTextView.setText(position + "");

        String path = list.get(position);
        File file = new File(path);
        String name = file.getName();

        holder.textView.setText(name);
    }

    @Override
    public int getItemCount() {
        return null == list ? 0 : list.size();
    }

    public void setData(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    class SoundHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        TextView textView;
        TextView indexTextView;
        ImageView playImageView;

        public SoundHolder(View itemView) {
            super(itemView);

            textView = itemView.findViewById(R.id.textView);
            indexTextView = itemView.findViewById(R.id.indexTextView);
            playImageView = itemView.findViewById(R.id.playImageView);

            playImageView.setOnClickListener(this);
            textView.setOnLongClickListener(this);
            textView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.playImageView: {
                    if (null != playClickListener && null != list && list.size() > 0) {
                        int position = getLayoutPosition();
                        String path = list.get(position);
                        playClickListener.onPlay(path);
                    }
                    break;
                }
                case R.id.textView: {
                    if (null != nameClickListener && null != list && list.size() > 0) {
                        int position = getLayoutPosition();
                        String path = list.get(position);
                        nameClickListener.onName(position, path);
                    }
                    break;
                }
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != nameLongClickListener && null != list && list.size() > 0) {
                int position = getLayoutPosition();
                String path = list.get(position);
                nameLongClickListener.onNameLongClick(position, path);
                return true;
            }
            return false;
        }
    }

    public interface OnPlayClickListener {
        void onPlay(String path);
    }

    public interface OnNameClickListener {
        void onName(int index, String path);
    }

    public interface OnNameLongClickListener {
        void onNameLongClick(int index, String path);
    }
}
